/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package qrlogin;

import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author adee
 */
public class QRLoginImage {
    private int width;
    private int height;
    private String sessionId;
    private Date created;
    private boolean isSaved;
    private QRLoginServlet servlet;


    private String getFilename(){
        return this.servlet.getServletParameters().getTempDir()+'/'+sessionId+".jpg";
    }


    public QRLoginImage(int width, int height, String sessionId, QRLoginServlet servlet) {
        this.width = width;
        this.height = height;
        this.sessionId = sessionId;
        this.isSaved = false;
        this.created = new Date();
        this.servlet = servlet;
    }

    public void saveMe(){

            BufferedImage image = generateQRImage(sessionId, this.servlet.getServletParameters().getLoginLink(), width, height);
            String fileName = this.getFilename();
            saveQRImage(image, fileName);
            this.isSaved = true;
    }

    public void deleteMe(){
        if (this.isSaved){
            new File(this.getFilename()).delete();

        }
    }

    public static BufferedImage generateQRImage(String sessionId, String loginLink, int width, int height){

        String data;
        data = sessionId+"\n"+loginLink;

         //get a byte matrix for the data
         BitMatrix matrix;
         com.google.zxing.Writer writer = new QRCodeWriter();
        try {
           matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, width, height);
         }
          catch (com.google.zxing.WriterException e) {
          //exit the method
          return null;
         }
         width = matrix.getWidth();
         height = matrix.getHeight();
          //create buffered image to draw to
         
         BufferedImage image = new BufferedImage(width, height+20, BufferedImage.TYPE_INT_RGB);
         for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
           int grayValue = matrix.get(x, y)? 0:0xffffffff;
           image.setRGB(x, y,grayValue);
         }
        }
        try {
            URL url = new URL(loginLink);
            Graphics2D g = image.createGraphics();
            Font font = new Font("Serif", Font.PLAIN, 20);
            g.setFont(font);
            g.setColor(Color.white);
            g.setFont(font);
            g.drawString(url.getHost(), 10,height+15);
        } catch (MalformedURLException ex) {
            Logger.getLogger(QRLoginImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    
        // image.getGraphics().drawString("Cica", 0,height );
         return image;
    }


    public static void saveQRImage(BufferedImage image, String filename){
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(new File(filename));
            ImageIO.write(image, "jpg", outputStream);
        } catch (IOException ex) {
            Logger.getLogger(QRLoginImage.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                outputStream.close();
            } catch (IOException ex) {
                Logger.getLogger(QRLoginImage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public String getMyPathSafe(){
        if (!this.isSaved) this.saveMe();
        return this.getFilename();
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public boolean isIsSaved() {
        return isSaved;
    }

    public void setIsSaved(boolean isSaved) {
        this.isSaved = isSaved;
    }


    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }



    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public String getImageSrc(){
        if (!this.isSaved) this.saveMe();
        return this.servlet.getServletParameters().getContextPath()+"/temp/"+this.sessionId+".jpg";
    }
    

}
